
package Test;

import java.io.File;
import java.util.ArrayList;

import junit.framework.TestCase;
import svgedit.gui.Frame;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;

/**
 *  Test case when ungroup several elements in a group and test when undo this execution
 *
 */
public class UnGroupElementTest extends TestCase {

	/**
	 *  Get Frame object
	 * */
	private static Frame frame;
	/**
	 *  Choose the elements which are in a group and store them in this container to be ungrouped
	 * */
	private ArrayList<SVGElement> elements = new ArrayList<SVGElement>();
	
	/**
	 *  The removed group
	 * */
	private SVGGroup group ;
	
	/**
	 *   Constructor for ungroup test
	 * */
	public UnGroupElementTest(String arg0) {
		super(arg0);
		frame = new Frame();
		File file = new File("src/testfile/test.svg");
        frame.openFile(file);
        group = getGroupedElement();
	}
	
	/**
	 *   Get a group object in this file
	 *   
	 *   @return group to get the group object
	 * */
	public SVGGroup getGroupedElement(){
		for (SVGElement elem : frame.getDocument()){
        	if (elem instanceof SVGGroup){
        		group = (SVGGroup)elem;
        		return group;
        	}
        }
		return null;
	}
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();	
	}
	
	/**
	 *  Test unit when group two element
	 * */
	public void testGroupElementTest(){
		GroupElement(elements);
	}
	
	/**
	 *  Test unit when undo the group execution
	 * */
	public void testUndoGroupElementTest(){
		undoGroupElement(elements);
	}
	
	/**
	 *  Undo the resizing operation to recover its original size
	 *  
	 *  @param elements the resized element
	 * */
	public void undoGroupElement(ArrayList<SVGElement> elements){
		
		for (SVGElement elem : group){
			group.removeChild(elem);
			group.getParent().appendChild(elem);
			
			assertEquals(frame.getDocument().getRootGroup(),elem.getParent());
		}
        frame.getDocument().getRootGroup().removeChild(group);
	}    
	
	/**
	 *  Group two elements
	 *  
	 *  @param elements the resized element
	 * */
	public void GroupElement( ArrayList<SVGElement> elements){
        for (SVGElement elem : elements) {
        	// Remove elements original parent
            elem.getParent().removeChild(elem);
            // Add these elements into a new group
            group.appendChild(elem);
            assertEquals(group,elem.getParent());
        }
        // Add this new group to the root
        frame.getDocument().getRootGroup().appendChild(group);
		
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
